<template>
	<view class="page">
		<headerinfo></headerinfo>
		<!-- <u-navbar title="设置" :placeholder="true" :autoBack="true"></u-navbar> -->
		<!-- <headertop :inv="5"></headertop> -->
				<backos></backos>
		<view class="changeMy">
			<u-cell title="编辑资料" :isLink="true" @click="toPage('/pages/tabbar/mine/editmy')"></u-cell>
			<u-cell title="账号与安全" :isLink="true" @click="toPage('/pages/tabbar/mine/accountsafe')"></u-cell>
			<view class="kaiguanlist">
				<span>首页展示共享文章</span>
				<u-switch @change="changeshowUserGuide" v-model="userData.showUserGuide"
					activeColor="#de4849"></u-switch>
			</view>
			<u-cell @click="clearStorage" title="清除缓存" :value="storageSize" :isLink="true"></u-cell>
		</view>
	
		<view class="loginOut" @click="loginOut">退出登录</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				storageSize: '',
				userData: {}
			};
		},
		onLoad() {
			this.getStorageSize();
			this.getData();
		},
		methods: {
			getData() {
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.myInfo().then(res => {
					this.userData = res;
					uni.hideLoading()
				});
			},
			changeshowUserGuide() {

				this.$api.upateUserInfo(this.userData).then(res => {
					uni.setStorageSync('needRefresh', 1)
					this.$Show.show('修改成功')
				})
			},
			loginOut() {
				uni.showModal({
					title: '退出确认',
					content: '退出当前帐号，将不能同步收藏，发布评论和云端分享等',
					confirmText: '确认退出',
					success: res => {
						if (res.confirm) {
							uni.removeStorageSync('token')
							uni.reLaunch({
								url: '/pages/login/index'
							});
						}
					}
				});
			},
			getStorageSize() {
				let that = this;
				uni.getStorageInfo({
					success(res) {
						let size = res.currentSize;
						if (size < 1024) {
							that.storageSize = size + ' B';
						} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
							that.storageSize = Math.floor((size / 1024) * 100) / 100 + ' KB';
						} else if (size / 1024 / 1024 >= 1) {
							that.storageSize = Math.floor((size / 1024 / 1024) * 100) / 100 + ' M';
						}
					}
				});
			},
			clearStorage() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定清除缓存吗?',
					confirmText: '立即清除',
					success(res) {
						if (res.confirm) {
							uni.clearStorageSync();
							//重新获取并显示清除后的缓存大小
							that.getStorageSize();
							uni.showToast({
								title: '清除成功'
							});
						}
					}
				});
			},
			toPage(url) {
				console.log(url);
				uni.navigateTo({
					url
				});
			}
		}
	};
</script>

<style>
	.page{
		background: #fff!important;
	}
</style>
<style lang="scss" scoped>
	.kaiguanlist {
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #e6e6e6;
		font-size: 30rpx;
	}

	.changeMy {
		border-top: 1px solid #f8f8f8;
		background-color: #fff;
		margin-top: 20rpx;
		width: 600px;
		margin: 0 auto;
		    box-shadow: 0 0 3px #ccc;
		    border-radius: 10px;
	}

	.loginOut {
		width: 690rpx;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		margin: 0 auto;
		margin-top: 40rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		background-color: #de4849;
	}
</style>